<template>
  <el-dialog
    :title="'抵押查询'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm"  v-loading="submitLoading"  :element-loading-text="'正在提交'"
    :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="130px">


    <el-form-item label="登记中心" prop="djzx"  v-if="false"   >
      <el-select v-model="dataForm.djzx" placeholder="登记中心" style="width:100%">
          <el-option
            v-for="item in dataForm.bankList"
            :key="item.bankid"
            :label="item.bankname"
            :value="item.bankid">
          </el-option>
      </el-select>
    </el-form-item>


    <el-form-item label="地市" prop="seRegion">
      <el-select  @change="selectChanged" v-model="dataForm.seRegion" placeholder="区域" style="width:100%">
          <el-option
            v-for="item in dataForm.seRegionList"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="区县" prop="region">
      <el-select v-model="dataForm.region" placeholder="区县" style="width:100%">
          <el-option
            v-for="item in dataForm.regionList"
            :key="item.code"
            :label="item.name"
            :value="item.code">
          </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="业务件号"  prop="ywjh" >
      <el-input v-model="dataForm.ywjh"   :disabled="dataForm.id == 1" placeholder="业务件号"></el-input>
    </el-form-item>

    <el-form-item label="不动产登记证明号" prop="bdcdjzmh"  v-if="dataForm.id >= -1" >
      <el-input v-model="dataForm.bdcdjzmh" :disabled="dataForm.id == 1"  placeholder="不动产登记证明号"></el-input>
    </el-form-item>

    <el-form-item label="不动产单元号"  prop="bdcdyh"  v-if="dataForm.id >= 0">
      <el-input v-model="dataForm.bdcdyh" :disabled="dataForm.id == 1" placeholder="不动产单元号"></el-input>
    </el-form-item>

      <el-form-item label="抵押权人机构"  prop="orgid">
              <el-select
                    v-model="dataForm.orgid"
                    clearable
                    filterable
                    style="width:100%"
                    remote
                    :disabled="dataForm.id == 1"
                    placeholder="输入抵押权人代码或名称"
                    :remote-method="remoteMethod"
                    :loading="loading">
                    <el-option
                      v-for="item in list"
                      :key="item.branchid"
                      :label="item.branchname"
                      :value="item.branchid">
                    </el-option>
                  </el-select>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()"  :disabled="submitLoading" >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {

      return {
        visible: false,
        loading: false,
        submitLoading: false,
        width:660,
        height:270,
        list:[],
        dataForm: {
          id: 0,
          url: '',
          bdczt: '',
          region: '',
          seRegion: '',
          regionList: [],
          seRegionList: [],
          bdcStatusList: [],
          bankList: [],
          bdcdjzmh: '',
          ywjh: '',
          bdcdyh: '',
          orgid: '',
          djzx: ''
        },
        dataRule: {

          region: [
            { required: true, message: '区县不能为空', trigger: 'blur' }
          ],
          seRegion: [
            { required: true, message: '地市不能为空', trigger: 'blur' }
          ],
          orgid: [
            { required: true, message: '抵押权人机构不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, seRegion, region, ywjh, bdcdjzmh, orgid) {
        this.dataForm.id = id || 0
        this.visible = true
        if (id == -1){
          this.dataForm.url = this.$http.adornUrl(`/bdc/cancelregist/query`)
        }else if (id == -2){
          this.dataForm.url = this.$http.adornUrl(`/bdc/manualyzb/query`)
        }
        else{
          this.dataForm.url = this.$http.adornUrl(`/bdc/bdcrealtyinfo/send`)
          if (id == 1){
             this.dataForm.region = region
             this.dataForm.bdcdjzmh = bdcdjzmh
             this.dataForm.orgid = orgid
             this.dataForm.seRegion = seRegion
             this.dataForm.ywjh = ywjh
          }
        }

        this.$http({
          url: this.$http.adornUrl(`/bdc/bdcrealtyinfo/item/list`),
              method: 'get',
              params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.seRegionList = data.regionList
              this.dataForm.bdcStatusList = data.bdcStatusList 
              this.dataForm.orgid = data.orgid
              this.dataForm.bankList = data.bankList
            }
        })

        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })

      },
      remoteMethod(query) {
        if (query !== '') {
          this.loading = true;

          this.$http({
            url: this.$http.adornUrl(`/sys/user/branch/search`),
            method: 'get',
            params: this.$http.adornParams({
              "key": query
            })
          }).then(({data}) => {
            this.loading = false;
            if (data && data.code === 0) {
                this.list = data.list;
            }
          })     
          
        } else {
          this.list = [];
        }
      },
      selectChanged(value){
          this.dataForm.region = '';
          this.$http({
          url: this.$http.adornUrl(`/sys/bdcregion/listByLevle`),
              method: 'get',
              params: this.$http.adornParams({'upregion':value})
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.regionList = data.list
            }
        })
      }
      ,

      // 表单提交
      dataFormSubmit () {
        

        
        if (!this.dataForm.bdcdyh && !this.dataForm.ywjh && !this.dataForm.bdcdjzmh){
            if (this.dataForm.id == '-1'){
                this.$message.error("业务件号、不动产登记证明号至少输入一项")
            }else{
                this.$message.error("业务件号、不动产登记证明号、不动产单元号至少输入一项")
            }
            
            return;
        }

        this.$refs['dataForm'].validate((valid) => {

          if (valid) {
            this.submitLoading = true
            var self = this;
            this.$http({
              url: this.dataForm.url,
              method: 'post',
              data: this.$http.adornData({
                // 'djzx': this.dataForm.djzx,
                'region': this.dataForm.region,
                'bdcdjzmh': this.dataForm.bdcdjzmh,
                'cxorgid': this.dataForm.orgid,
                'seRegion': this.dataForm.seRegion,
                'ywjh': this.dataForm.ywjh,
                'bdcdyh': this.dataForm.bdcdyh

              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '查询成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    if (self.dataForm.id == -1){
                        self.$emit('refreshDataList', self.dataForm.orgid, self.dataForm.ywjh, self.dataForm.bdcdjzmh, self.dataForm.region)
                    }else if (self.dataForm.id == -2){
                        self.$emit('refreshDataList', self.dataForm.orgid, self.dataForm.ywjh, self.dataForm.region)
                    }else if (self.dataForm.id == 1){
                        self.$router.push({ name: 'bdc-bdcrealtyinfo' })
                    }
                    else{
                        self.$emit('refreshDataList')
                    }   
                                
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
                this.submitLoading = false   
            }).catch(err => {
              this.submitLoading = false;
              console.error(err);
              this.$message.error('系统异常')              
	        	}); 
          }
        })
      }
    }
  }
</script>
<style>
.el-input.is-disabled .el-input__inner {
    background-color: #f5f7fa;
    border-color: #e4e7ed;
    color: #313b50;
    cursor: not-allowed;
}
</style>